package com.pktechan.api.controller;

import com.pktechan.api.entity.MiniSubscribeMessageRecord;
import com.pktechan.api.exception.CustomException;
import com.pktechan.api.service.MiniSubscribeMessageRecordService;
import com.pktechan.api.utils.HtmlUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @program: cloudfloating
 * @description: 消息订阅记录
 * @author: qx.zhang
 * @create: 2021-06-23 11:28
 **/
@RestController
@RequestMapping("miniSubscribeMessageRecordController")
public class MiniSubscribeMessageRecordController {

    private static final Logger logger = LoggerFactory.getLogger(MiniSubscribeMessageRecordController.class);

    @Autowired
    private MiniSubscribeMessageRecordService miniSubscribeMessageRecordService;

    /**
     * 保存或更新消息订阅记录
     *
     * @param miniSubscribeMessageRecord
     * @param response
     */
    @RequestMapping("saveOrUpdate")
    public void saveOrUpdate(MiniSubscribeMessageRecord miniSubscribeMessageRecord, HttpServletResponse response) {
        try {
            logger.info("保存或更新消息订阅记录");
            miniSubscribeMessageRecordService.saveOrUpdate(miniSubscribeMessageRecord);
            HtmlUtil.writerSuccessJson(response);
        } catch (CustomException e) {
            e.printStackTrace();
            logger.debug(e.getErrMsg());
            HtmlUtil.writerFailJson(response, e.getErrMsg());
        } catch (Exception e) {
            e.printStackTrace();
            logger.debug(e.getMessage());
            HtmlUtil.writerFailJson(response, e.getMessage());
        }
    }

    /**
     * 根据openId查询订阅消息集合，未订阅的不查询
     *
     * @param openId
     * @param response
     */
    @RequestMapping("getListByOpenId")
    public void getListByOpenId(String openId, HttpServletResponse response) {
        try {
            List<MiniSubscribeMessageRecord> list = miniSubscribeMessageRecordService.getListByOpenId(openId);
            HtmlUtil.writerSuccessJson(response, list);
        } catch (CustomException e) {
            e.printStackTrace();
            logger.debug(e.getErrMsg());
            HtmlUtil.writerFailJson(response, e.getErrMsg());
        } catch (Exception e) {
            e.printStackTrace();
            logger.debug(e.getMessage());
            HtmlUtil.writerFailJson(response, e.getMessage());
        }
    }

}
